function groupBy(array, f) {
  let groups = {};
  array.forEach(function (o) {
    let group = f(o);
    groups[group] = groups[group] || [];
    groups[group].push(o);
  });
  var list = Object.keys(groups).map(function (group) {
    return { k: group, v: groups[group] };
  });
  return list.sort(function (i1, i2) {
    if (i1.k > i2.k) {
      return 1;
    } else if (i1.k < i2.k) {
      return -1;
    } else {
      return 0;
    }
  })
}

function strBytes(c) {
  var n = c.length, s;
  var len = 0;
  for (var i = 0; i < n; i++) {
    s = c.charCodeAt(i);
    while (s > 0) {
      len++;
      s = s >> 8;
    }
  }
  return len;
}

function isBlank(str) {
  return (!str || /^\s*$/.test(str));
}

function isPhone(phone) {
  var reg = /^1[0-9]{10}$/;
  var format = reg.test(phone);
  return format;
}

function isExist(obj) {
  return (typeof obj != 'undefined' && obj);
}

function isEmpty(obj) {
  for (var name in obj) {
    return false;
  }
  return true;
};

function showToast(txt){
	uni.showToast({
		title: txt,
		icon: "none"
	})
}

function showErr(title){
	uni.showToast({
		icon: 'none',
		title: isBlank(title)?"请求失败":title,
		duration:1500,
		mask:true
	});
}

function showLoading(title){
	uni.showLoading({
		mask: true,
		title:title || ""
	})
}

function pUrl() {
	var pages = getCurrentPages()    //获取加载的页面
	var currentPage = pages[pages.length - 1]    //获取当前页面的对象
	var url = currentPage.route    //当前页面url
	return url
}

module.exports = {
	isBlank,
	isPhone,
	isExist,
	isEmpty,
	strBytes,
	pUrl,
	showLoading,
	showErr,
	groupBy,
}